package edu.pku.sei.act.capabilityprovider;

import edu.pku.sei.act.actools.ICapabilityManager;
import edu.pku.sei.act.actools.ICapabilityProvider;
import edu.pku.sei.act.actools.IEnvEntry;
import edu.pku.sei.act.actools.util.ACToolsNamingSpace;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class CapabilityProviderService extends Service {

//	private static ICapabilityManager cm;
//	private static IEnvEntry ee;
//
//	private ServiceConnection CMConnection = new ServiceConnection() {
//
//		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
//
//			cm = ICapabilityManager.Stub.asInterface(arg1);
//		}
//
//		public void onServiceDisconnected(ComponentName name) {
//
//			cm = null;
//		}
//	};
//	
//	private ServiceConnection EEConnection = new ServiceConnection() {
//
//		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
//
//			ee = IEnvEntry.Stub.asInterface(arg1);
//		}
//
//		public void onServiceDisconnected(ComponentName name) {
//
//			ee = null;
//		}
//	};

	@Override
	public void onCreate() {
		
		super.onCreate();
		Log.v("CapabilityProviderService","onCreate");
//		final Intent serviceIntent1 = new Intent();
//		serviceIntent1
//				.setAction(ACToolsNamingSpace.ACToolsComponent.CAPABILITY_MANAGER);
//		bindService(serviceIntent1, CMConnection, Context.BIND_AUTO_CREATE);
//
//		final Intent serviceIntent2 = new Intent();
//		serviceIntent2.setAction(ACToolsNamingSpace.ACToolsComponent.ENV_ENTRY);
//		bindService(serviceIntent2, EEConnection, Context.BIND_AUTO_CREATE);

	}

	@Override
	public void onDestroy() {
	//	unbindService(CMConnection);
	//	unbindService(EEConnection);
		super.onDestroy();
	}
	private final ICapabilityProvider.Stub mbinder = new ICapabilityProvider.Stub() {

		public String invoke(String abilityName, String input)
				throws RemoteException {
			Log.v("CapabilityProviderService","invoking " + abilityName);
			if (abilityName.equals(_Capabilities.PROVIDE_POSITION.getAbilityName())) {
				String ret = "ParkingPosition("+Math.random()+"-" + Math.random()+")";
				Log.v("CapabilityProviderService","return:" + ret);
				return ret;
			}
			return null;
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return mbinder;
	}

}
